package com.kitty.game.rank;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.rank.model.param.RankReqParam;
import com.kitty.game.rank.model.pojo.RankViewData;
import com.kitty.game.rank.model.record.RankRecord;
import com.kitty.game.rank.service.RankService;
import com.kitty.game.rank.service.handler.*;
import com.kitty.mina.message.Message;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import com.kitty.game.rank.service.handler.BullyRankHandler;
import com.kitty.game.rank.service.handler.ChildLevelRankHandler;
import com.kitty.game.rank.service.handler.DefenseRankHandler;
import com.kitty.game.rank.service.handler.EquipRankHandler;
import com.kitty.game.rank.service.handler.FeiXianDuXieRankHandler;
import com.kitty.game.rank.service.handler.FuMoRankHandler;
import com.kitty.game.rank.service.handler.MagPowerRankHandler;
import com.kitty.game.rank.service.handler.MonthTaoRankHandler;
import com.kitty.game.rank.service.handler.PartyCapitalRankHandler;
import com.kitty.game.rank.service.handler.PetRankHandler;
import com.kitty.game.rank.service.handler.PhyPowerRankHandler;
import com.kitty.game.rank.service.handler.RoleLevelRankHandler;
import com.kitty.game.rank.service.handler.SpeedRankHandler;
import com.kitty.game.rank.service.handler.TaoRankHandler;
import com.kitty.game.rank.service.handler.TowerRankHandler;
import com.kitty.game.rank.service.handler.XiangYaoRankHandler;
import com.kitty.game.rank.service.handler.YingXiongRankHandler;
import com.kitty.game.rank.service.handler.ZhengDaoRankHandler;

public class RankDataPool {
    /**key: 排行榜类型, value: 获得对应排行榜的方法*/
    public static Map<Short, Function<RankReqParam, Message>> rankHanlers = new HashMap<>();

    public static Map<Serializable, Function<String, List<RankRecord>>> kfRankHandlers = new HashMap<>();

    /**key: gid, value: 显示的信息对象 */
    public static Map<String, RankViewData> rankViewMap = new ConcurrentHashMap<>();

    static {
        rankHanlers.put((short) 101, (rankReqParam -> SpringUtils.getBean(RoleLevelRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 102, (rankReqParam -> SpringUtils.getBean(TaoRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 103, (rankReqParam -> SpringUtils.getBean(PhyPowerRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange()+"-103")));
        rankHanlers.put((short) 104, (rankReqParam -> SpringUtils.getBean(MagPowerRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange()+"-104")));
        rankHanlers.put((short) 105, (rankReqParam -> SpringUtils.getBean(SpeedRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange()+"-105")));
        rankHanlers.put((short) 106, (rankReqParam -> SpringUtils.getBean(DefenseRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange()+"-106")));
        rankHanlers.put((short) 107, (rankReqParam -> SpringUtils.getBean(ChildLevelRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 108, (rankReqParam -> SpringUtils.getBean(MonthTaoRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 111, (rankReqParam -> SpringUtils.getBean(ZhanLiRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange()+"-111")));
        rankHanlers.put((short) 602, (rankReqParam -> SpringUtils.getBean(XiangYaoRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 603, (rankReqParam -> SpringUtils.getBean(FuMoRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 604, (rankReqParam -> SpringUtils.getBean(FeiXianDuXieRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 606, (rankReqParam -> SpringUtils.getBean(FuMo2RankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 607, (rankReqParam -> SpringUtils.getBean(FeiXianDuXie2RankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 1101, (rankReqParam -> SpringUtils.getBean(ZhengDaoRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 1102, (rankReqParam -> SpringUtils.getBean(ZhengDaoRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 1103, (rankReqParam -> SpringUtils.getBean(ZhengDaoRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 1104, (rankReqParam -> SpringUtils.getBean(ZhengDaoRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 1105, (rankReqParam -> SpringUtils.getBean(ZhengDaoRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 702, (rankReqParam -> SpringUtils.getBean(TowerRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 301, (rankReqParam -> SpringUtils.getBean(PetRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 302, (rankReqParam -> SpringUtils.getBean(PetRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 303, (rankReqParam -> SpringUtils.getBean(PetRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 304, (rankReqParam -> SpringUtils.getBean(PetRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 305, (rankReqParam -> SpringUtils.getBean(PetRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 306, (rankReqParam -> SpringUtils.getBean(PetRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 307, (rankReqParam -> SpringUtils.getBean(PetRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 201, (rankReqParam -> SpringUtils.getBean(EquipRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 202, (rankReqParam -> SpringUtils.getBean(EquipRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 203, (rankReqParam -> SpringUtils.getBean(EquipRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 204, (rankReqParam -> SpringUtils.getBean(EquipRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 1201, (rankReqParam -> SpringUtils.getBean(YingXiongRankHandler.class).getRankResponse(rankReqParam.getType(), rankReqParam.getRange())));
        rankHanlers.put((short) 501, (rankReqParam -> SpringUtils.getBean(PartyCapitalRankHandler.class).getRankResponse(rankReqParam.getType())));
        rankHanlers.put((short) 801, (rankReqParam -> SpringUtils.getBean(BullyRankHandler.class).getRankResponse(rankReqParam.getType())));
        //鬼宠战力
        rankHanlers.put((short) 1305, (rankReqParam -> SpringUtils.getBean(PetRankHandler.class).getRankResponse(rankReqParam.getType())));
        //鬼宠物伤
        rankHanlers.put((short) 1301, (rankReqParam -> SpringUtils.getBean(PetRankHandler.class).getRankResponse(rankReqParam.getType())));
        //鬼宠法伤
        rankHanlers.put((short) 1302, (rankReqParam -> SpringUtils.getBean(PetRankHandler.class).getRankResponse(rankReqParam.getType())));
        //鬼宠速度
        rankHanlers.put((short) 1303, (rankReqParam -> SpringUtils.getBean(PetRankHandler.class).getRankResponse(rankReqParam.getType())));
        //鬼宠防御
        rankHanlers.put((short) 1304, (rankReqParam -> SpringUtils.getBean(PetRankHandler.class).getRankResponse(rankReqParam.getType())));

        addKfRankHandler((short) 102, new String[]{"45-79", "80-89", "90-99", "100-109", "110-119", "120-129", "130-139", "140-149", "150-159", "160-169", "170-179"}, (range) -> SpringUtils.getBean(TaoRankHandler.class).getRecordsFrom(range));
        addKfRankHandler((short) 108, new String[]{"45-79", "80-89", "90-99", "100-109", "110-119", "120-129", "130-139", "140-149", "150-159", "160-169", "170-179"}, (range) -> SpringUtils.getBean(MonthTaoRankHandler.class).getRecordsFrom(range));
        addKfRankHandler((short) 103, new String[]{"45-79-103", "80-89-103", "90-99-103", "100-109-103", "110-119-103", "120-129-103", "130-139-103", "140-149-103", "150-159-103", "160-169-103", "170-179-103"}, (range) -> SpringUtils.getBean(PhyPowerRankHandler.class).getRecordsFrom(range));
        addKfRankHandler((short) 104, new String[]{"45-79-104", "80-89-104", "90-99-104", "100-109-104", "110-119-104", "120-129-104", "130-139-104", "140-149-104", "150-159-104", "160-169-104", "170-179-104"}, (range) -> SpringUtils.getBean(MagPowerRankHandler.class).getRecordsFrom(range));
        addKfRankHandler((short) 105, new String[]{"45-79-105", "80-89-105", "90-99-105", "100-109-105", "110-119-105", "120-129-105", "130-139-105", "140-149-105", "150-159-105", "160-169-105", "170-179-105"}, (range) -> SpringUtils.getBean(SpeedRankHandler.class).getRecordsFrom(range));
        addKfRankHandler((short) 106, new String[]{"45-79-106", "80-89-106", "90-99-106", "100-109-106", "110-119-106", "120-129-106", "130-139-106", "140-149-106", "150-159-106", "160-169-106", "170-179-106"}, (range) -> SpringUtils.getBean(DefenseRankHandler.class).getRecordsFrom(range));
        addKfRankHandler((short) 111, new String[]{"45-79-111", "80-89-111", "90-99-111", "100-109-111", "110-119-111", "120-129-111", "130-139-111", "140-149-111", "150-159-111", "160-169-111", "170-179-111"}, (range) -> SpringUtils.getBean(ZhanLiRankHandler.class).getRecordsFrom(range));
        addKfRankHandler((short) 201, new String[]{"70-79", "80-89", "90-99", "100-109", "110-119", "120-129", "130-139", "140-149", "150-159", "160-169", "170-179"}, (range) -> SpringUtils.getBean(EquipRankHandler.class).getRecordsFrom((short) 201, range));
        addKfRankHandler((short) 202, new String[]{"70-79", "80-89", "90-99", "100-109", "110-119", "120-129", "130-139", "140-149", "150-159", "160-169", "170-179"}, (range) -> SpringUtils.getBean(EquipRankHandler.class).getRecordsFrom((short) 202, range));
        addKfRankHandler((short) 203, new String[]{"70-79", "80-89", "90-99", "100-109", "110-119", "120-129", "130-139", "140-149", "150-159", "160-169", "170-179"}, (range) -> SpringUtils.getBean(EquipRankHandler.class).getRecordsFrom((short) 203, range));
        addKfRankHandler((short) 204, new String[]{"70-79", "80-89", "90-99", "100-109", "110-119", "120-129", "130-139", "140-149", "150-159", "160-169", "170-179"}, (range) -> SpringUtils.getBean(EquipRankHandler.class).getRecordsFrom((short) 204, range));
        addKfRankHandler((short) 301, (range) -> SpringUtils.getBean(PetRankHandler.class).getRecordsFrom((short) 301));
        addKfRankHandler((short) 306, (range) -> SpringUtils.getBean(PetRankHandler.class).getRecordsFrom((short) 306));
        addKfRankHandler((short) 307, (range) -> SpringUtils.getBean(PetRankHandler.class).getRecordsFrom((short) 307));
        addKfRankHandler((short) 1305, (range) -> SpringUtils.getBean(PetRankHandler.class).getRecordsFrom((short) 1305));
        addKfRankHandler((short) 602, (range) -> SpringUtils.getBean(XiangYaoRankHandler.class).getRecordsFrom( ));
        addKfRankHandler((short) 603, (range) -> SpringUtils.getBean(FuMoRankHandler.class).getRecordsFrom());
        addKfRankHandler((short) 604, (range) -> SpringUtils.getBean(FeiXianDuXieRankHandler.class).getRecordsFrom());
        //二阶附魔
        addKfRankHandler((short) 606, (range) -> SpringUtils.getBean(FuMo2RankHandler.class).getRecordsFrom());
        //二阶飞仙
        addKfRankHandler((short) 607, (range) -> SpringUtils.getBean(FeiXianDuXie2RankHandler.class).getRecordsFrom());
    }

    private static void addKfRankHandler(short type, String[] levelRanges, Function<String, List<RankRecord>> function) {
        if (levelRanges != null) {
            for (String levelRange : levelRanges) {
                String key = SpringUtils.getBean(RankService.class).getKey(type, levelRange);
                kfRankHandlers.put(key, function);
            }
        } else {
            addKfRankHandler(type, function);
        }
    }

    private static void addKfRankHandler(short type, Function<String, List<RankRecord>> function) {
        String key = SpringUtils.getBean(RankService.class).getKey(type, null);
        kfRankHandlers.put(key, function);
    }
}
